package com.supinfo.sun.sungamers.games;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.util.Scanner;

import com.supinfo.sun.sungamers.enums.Level;
import com.supinfo.sun.sungamers.exceptions.InvalidUserEntryException;
import com.supinfo.sun.sungamers.exceptions.UserAttemptException;
import com.supinfo.sun.sungamers.threads.DurationGameRunnable;

/**
 * @author Liu, Bing(Locke)
 * @since 2011-04-05
 * @version 0.3
 */
public class NumberFinderGame extends FinderGame {

	int numberToFind;
	List<Integer> usersAttempts;
	static Random randomGenerator;

	public NumberFinderGame(Level level) {
		this.level = level;
	}

	public void initGame() {
		super.initGame();
		// try {
		// Socket client = new Socket("localhost" ,33000);
		// InputStream in = client.getInputStream();
		// byte[] buffer = new byte[64];
		// String rule = null;

		// while(in.read(buffer) != -1) {
		// rule = new String(buffer);
		// System.out.println(rule);
		// }
		// } catch (UnknownHostException e) {
		// System.out.println("CapitalFinderGame UnknownHostException"
		// + e.getMessage());
		// } catch (IOException e) {
		// System.out
		// .println("CapitalFinderGame IOException" + e.getMessage());
		// }
		numberToFind = getRandomGenerator().nextInt(100); // create a random
															// number
		System.out.println("numberToFind: " + numberToFind);

		usersAttempts = new ArrayList<Integer>();
	}

	public Object readUserEntry() throws InvalidUserEntryException,
			InputMismatchException {
		Scanner scan = new Scanner(System.in);
		int tempNumber = scan.nextInt();
		if (tempNumber < 0)
			throw new InvalidUserEntryException();
		return tempNumber;

	}

	public void displayFinalMessage() {
		super.displayFinalMessage();

		ListIterator<Integer> it = usersAttempts.listIterator();

		while (it.hasNext()) {
			int userAttempt = it.next(); // wrapper: Integer
			System.out.println("The attempt is: " + userAttempt);
		}

		usersAttempts.clear();

		System.out.println("**********Game Over**********");
	}

	public synchronized void play() {
		DurationGameRunnable gameRunnable = new DurationGameRunnable();
		Thread thread = new Thread(gameRunnable);
		thread.setPriority(Thread.MAX_PRIORITY);
		thread.start();

		System.out.println("Pleast enter a number between 0 and 100...");
		int index = 0;
		while (allowedAttempts > 0 && won != true) {
			allowedAttempts--;
			try {
				usersAttempts.add((Integer) readUserEntry());
			} catch (InputMismatchException ex) {
				usersAttempts.add(-1);
				System.out.println("Invalid Entry");
			} catch (InvalidUserEntryException e) {
				System.out.println("Invalid Entry");
			} finally {
			}

			ListIterator<Integer> it = usersAttempts.listIterator();

			int usersAttempt = usersAttempts.get(index);
			if (usersAttempt == numberToFind) {
				won = true;
			} else if (usersAttempt < numberToFind && allowedAttempts > 0) {
				System.out.println("Please enter a great value(0 ~ 100)");
			} else if (usersAttempt > numberToFind && allowedAttempts > 0) {
				System.out.println("Please enter a small value(0 ~ 100)");
			}
			index++;
		}

		synchronized (gameRunnable) {
			gameRunnable.notify();
		}
		displayFinalMessage();
	}

	static Random getRandomGenerator() {
		if (randomGenerator == null) {
			randomGenerator = new Random();
		}
		return randomGenerator;
	}

	@Override
	public void addUserAttempt(Object userEntry) throws UserAttemptException {
		if (allowedAttempts > 0) {
			if (numberToFind == Integer.parseInt((String) userEntry)) {
				won = true;
			} else {
				won = false;
			}
			allowedAttempts--;
		} else
			throw new UserAttemptException("number finder game failed...");
	}
}
